package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.ws.WebSocketServer;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@RocketMQMessageListener(
    consumerGroup = MQConstans.CREATE_ORDER_NOTIFY,
    topic = MQConstans.CREATE_ORDER,
    selectorExpression = MQConstans.CREATE_FAIL_TAG +"||"+ MQConstans.CREATE_SUCCESS_TAG
)
@Component
public class OrderFailNotifyMQListener implements RocketMQListener<Map> {

    @Override
    public void onMessage(Map map) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String uuid = (String) map.get("uuid");
        WebSocketServer webSocketServer = WebSocketServer.clients.get(uuid);
        if (webSocketServer!=null){
            try {
                webSocketServer.getSession().getBasicRemote().sendText(JSON.toJSONString(map));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
